﻿Function.prototype.bind = function (object) {
    var _method = this;
    return function () {
        _method.apply(object, arguments);
    };
};
/**
* 扩展String方法
*/
$.extend(String.prototype, {
    nocache : function () {
        var str = this;
        if (this.indexOf("?") == -1) {
            str = str + "?rnd=" + new Date().getTime();
        } else if (this.indexOf("&") == -1) {
            str = str + "&rnd=" + new Date().getTime();
        }
        return str;
    },
    isPositiveInteger: function () {
        return (new RegExp(/^[1-9]\d*$/).test(this));
    },
    isInteger: function () {
        return (new RegExp(/^\d+$/).test(this));
    },
    isNumber: function (value, element) {
        return (new RegExp(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/).test(this));
    },
    trim: function () {
        return this.replace(/(^\s*)|(\s*$)|\r|\n/g, "");
    },
    startsWith: function (pattern) {
        return this.indexOf(pattern) === 0;
    },
    endsWith: function (pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
    },
    replaceSuffix: function (index) {
        return this.replace(/\[[0-9]+\]/, '[' + index + ']').replace('#index#', index);
    },
    trans: function () {
        return this.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"');
    },
    encodeTXT: function () {
        return (this).replaceAll('&', '&amp;').replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll(" ", "&nbsp;");
    },
    replaceAll: function (os, ns) {
        return this.replace(new RegExp(os, "gm"), ns);
    },
    replaceTm: function ($data) {
        if (!$data) return this;
        return this.replace(RegExp("([{\\(][A-Za-z_]+[A-Za-z0-9_]*[}\\)])", "g"), function ($1) {
            return $data[$1.replace(/[{}\\(\\)]+/g, "")];
        });
    },
    replaceTmById: function (_box) {
        var $parent = _box || $(document);
        return this.replace(RegExp("([{\\(][A-Za-z_]+[A-Za-z0-9_]*[}\\)])", "g"), function ($1) {
            var $pk = $parent.find("input:checkbox[rel=" + $1.replace(/[{}\\(\\)]+/g, "") + "]:checked:eq(0)");
            if ($pk.size() > 0) {
                return $pk.val();
            }
            var $input = $parent.find("#" + $1.replace(/[{}\\(\\)]+/g, ""));
            return $input.val() ? $input.val() : $1;
        });
    },
    isFinishedTm: function () {
        return !(new RegExp("[{\\(][A-Za-z_]+[A-Za-z0-9_]*[}\\)]").test(this));
    },
    skipChar: function (ch) {
        if (!this || this.length === 0) { return ''; }
        if (this.charAt(0) === ch) { return this.substring(1).skipChar(ch); }
        return this;
    },
    isValidPwd: function () {
        return (new RegExp(/^([_]|[a-zA-Z0-9]){6,32}$/).test(this));
    },
    isValidMail: function () {
        return (new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(this.trim()));
    },
    isSpaces: function () {
        for (var i = 0; i < this.length; i += 1) {
            var ch = this.charAt(i);
            if (ch != ' ' && ch != "\n" && ch != "\t" && ch != "\r") { return false; }
        }
        return true;
    },
    isPhone: function () {
        return (new RegExp(/(^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)/).test(this));
    },
    isUrl: function () {
        return (new RegExp(/^[a-zA-z]+:\/\/([a-zA-Z0-9\-\.]+)([-\w .\/?%&=:]*)$/).test(this));
    },
    isExternalUrl: function () {
        return this.isUrl() && this.indexOf("://" + document.domain) == -1;
    }
});

function List() {
    this.a = [];
    this.push = function (s) {
        var self = this;
        this.a.push(s);
        return this;
    };
    this.pop = function () {
        if (this.a.length == 0) return null;
        var s = this.a[this.a.length - 1];
        this.a.pop();
        return s;
    };
    this.remove = function (expression) {
        if (!isNaN(expression)) {
            this.a = [].concat(this.a.slice(0, expression), this.a.slice(expression + 1));
            return this.a;
        } else {
            for (var i = 0; i < this.a.length; i++) {
                if (expression(this.a[i])) {
                    return this.remove(i);
                }
            }
        }
        return this.a;
    };
    this.get = function (index) {
        if (this.a.length - 1 < index) return null;
        return this.a[index];
    };
    this.exists = function (expression) {
        if (expression == null) return false;
        for (var i = 0; i < this.a.length; i++) {
            if (expression(this.a[i])) {
                return i;
            }
        }
        return false;
    };
    this.getTop = function () {
        if (this.a.length == 0) return null;
        return this.a[this.a.length - 1];
    };
    this.getIndex = function (expression) {
        if (expression == null) return null;
        for (var i = 0; i < this.a.length; i++) {
            if (expression(this.a[i])) {
                return i;
            }
        }
        return null;
    };
    this.first = function (expression) {
        if (expression == null) return null;
        for (var i = 0; i < this.a.length; i++) {
            if (expression(this.a[i])) {
                return this.a[i];
            }
        }
        return null;
    };
    this.swap = function (index1, index2) {
        var temp = this.a[index1];
        this.a[index1] = this.a[index2];
        this.a[index2] = temp;
    };
    this.moveToTop = function (index) {
        if (isNaN(index)) return;
        if (this.a.length - 1 < index) return;
        var o = this.get(index);
        this.remove(index);
        this.push(o);
    };
    this.length = function () {
        return this.a.length;
    };
}
var Utils = {
    getArgs: function (strParame) {
        var args = new Object();
        var query = location.search.substring(1); // Get query string
        var pairs = query.split("&"); // Break at ampersand
        for (var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('='); // Look for "name=value"
            if (pos == -1) continue; // If not found, skip
            var argname = pairs[i].substring(0, pos); // Extract the name
            var value = pairs[i].substring(pos + 1); // Extract the value
            value = decodeURIComponent(value); // Decode it, if needed
            args[argname] = value; // Store as a property
        }
        return args[strParame]; // Return the object
    },
    getGuid: function () {
        return "X" + new Date().getTime();
    },
    getAttrObject: function (jo, attrname) {
        var attrobj = {};
        if (jo.attr(attrname)) {
            eval("attrobj=" + jo.attr(attrname));
        }
        return attrobj;
    },
    eval: function (str) {
        return eval("(" + str + ")");
    },
    loadUrlto: function (url, jo, frompointerid, downintent, callback) {

        var iframe = document.createElement("iframe");
        iframe.src = url.nocache();
        iframe.contentWindowUpPointerid = frompointerid;
        iframe.contentWindowPointerid = Utils.getGuid();
        iframe.contentWindowPointerDownintent = downintent;
        iframe.callback = callback;
        $(iframe).css({ border: 0, margin: 0, padding: 0 })
                 .attr({ frameborder: "none", marginheight: 0, marginwidth: 0, paddingheight: 0, paddingwidth: 0, height: "100%", width: "100%" });
        $(iframe).load(function () {
            if (iframe.callback) iframe.callback(iframe);
            //this.contentWindow.mWindowPointer.up(this.contentWindowUpPointerid);
            // this.contentWindow.mWindowPointer.id(this.contentWindowPointerid);
            //if (this.downintent) this.contentWindow.mWindowPointer.downintent(this.contentWindowPointerDownintent);
            try {
                mPointerManager.register(iframe.contentWindow.mWindowPointer);
            } catch (ex) { }
        });
        jo.append($(iframe));
    },
    getGridHeight: function () {
        var mDivheight = 0;
        if ($(".flexigrid .mDiv").size() > 0) {
            mDivheight = 24;
        }
        return $(top.window).height() - 70 - 58 - $(".searchbar").outerHeight() - $(".actionbar").outerHeight() - mDivheight;
    }
}
window.laytime = 0;
(function ($) {
    $.fn.layout = function (fn) {
        if (fn == null) {
            return this.each(function () {
                window.laytime++;
                $(this).trigger("layout");
                $(this).find(".layoutable").trigger("layout");
            });
        } else {
            return this.each(function () {
                $(this).attr("layoutable", "1");
                $(this).addClass("layoutable");
                $(this).on("layout", fn);
            });
        }
    };
    $.fn.helper = function (name, fn) {
        if (fn != null) {
            this.data("helper_" + name, fn);
        } else {
            return this.data("helper_" + name);
        }
    };
    $.setRegional = function (key, value) {
        if (!$.regional) $.regional = {};
        $.regional[key] = value;
    };
    $.fn.numberinput = function () {
        return this.each(function () {
            $(this).css("ime-mode", "disabled");
            $(this).keyup(function () {
                $(this).val($(this).val().replace(/[^\d\.\-]/g, ''));
            });
        });
    };
    $.fn.intinput = function () {
        return this.each(function () {
            $(this).css("ime-mode", "disabled");
            $(this).keyup(function () {
                $(this).val($(this).val().replace(/[^\d]/g, ''));
            });
        });
    };
})(jQuery);